package com.wnhz.work.yz.utils;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

/** API返回结果解析 */
public class Analysis {
    /** 用户信息 */

    /** 上下文 */
    private Context context;
    /** Intent跳转的界面 */
    private Class<?> cls;
    /** Intent跳转的传递的数据 */
    private Object object=null;
    /** Intent跳转的标识 */
    private String action="";
    /**对话框中接口*/
    private Dispose dis;
    /**子栏目的数据*/


    private JSONArray array=null;
    private JSONObject fatherObject=null;
    private JSONObject childrenObject=null;
    /**
     * 构造器
     * @param context
     */
    public Analysis(Context context) {
        this.context = context;
    }
    /**
     * 用Intent跳转
     * @param action
     * @param cls  跳转到的页面
     * @param context
     * @param object
     */
    public Analysis(Context context, Class<?> cls, String action, Object object) {
        this.action = action;
        this.cls = cls;
        this.context = context;
        this.object = object;
    }
    /**
     * 对话框中有跳转
     * @param title
     * @param positive
     */
    public void dialogue (String title, String content, final String positive, final boolean positiveIntent){
        AlertDialog.Builder dialog=new AlertDialog.Builder(context);
        dialog.setTitle(title);
        dialog.setMessage(content);
        dialog.setPositiveButton(positive, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (positiveIntent){
                    /** 界面跳转*/
                    Intent intent=new Intent(context,cls);
                    if ((!action.equals("")&&action!=null)&&object!=null){
                        intent.putExtra(action, String.valueOf(object));
                    }
                    context.startActivity(intent);
                } else {
                    dis.dispose();
                }
            }
        });
        dialog.setNegativeButton("取消",null);
        dialog.create().show();
    }
    public interface Dispose {
        public void dispose();
    }
    /**
     * 对话框中没有跳转,1S后自动消失
     * @param title
     */
    public void dialogue (String title){
        Toast toast=Toast.makeText(context,title,Toast.LENGTH_LONG);
        LinearLayout linearLayout= (LinearLayout) toast.getView();
//        linearLayout.setBackgroundResource(R.drawable.text_view_border);
        TextView textView = (TextView) toast.getView().findViewById(android.R.id.message);
        textView.setGravity(Gravity.CENTER);
        textView.setWidth(700);
        textView.setHeight(62);
        textView.setTextSize(12);
//        textView.setTextColor(ContextCompat.getColor(context,R.color.white));
        toast.show();
    }


}
